<?php

namespace app\admin\controller\scheme;

use app\admin\model\ColumnClist;
use app\admin\model\SchemeShow;
use app\common\controller\Adminds;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="scheme_show")
 */
class Show extends Adminds
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new SchemeShow();
        
    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            foreach ($list as $v){
                $name_list = ColumnClist::whereIn('id',$v['fields'])->column('name');

                $v['fields'] = $name_list;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();


            $fields = array_keys($post['fields']);
            $select_data = ColumnClist::where('type',$post['type'])->whereIn('id',$fields)->column('id');
            if(empty($select_data)) $this->error('请勾选对应方案类型要展示的内容');
            $info = $this->model->where('name',$post['name'])->find();
            if($info) $this->error('方案名称已存在');
            $info = $this->model->where('type',$post['type'])->find();
            if($info) $this->error('该类型的方案已存在，请删除或重新编辑');
            $post['fields'] = $select_data;
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $fields = array_keys($post['fields']);
            $select_data = ColumnClist::where('type',$post['type'])->whereIn('id',$fields)->column('id');
            if(empty($select_data)) $this->error('请勾选对应方案类型要展示的内容');
            $info = $this->model->where('name',$post['name'])->find();
            if($info && $info['id'] != $id) $this->error('方案名称已存在');
            $info = $this->model->where('type',$post['type'])->find();
            if($info && $info['id'] != $id) $this->error('该类型的方案已存在，请删除或重新编辑');
            $post['fields'] = $select_data;
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        return $this->fetch();
    }
    
}